package com.dinadi.yidi.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.OrderDetailsRequest;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/11/7
 * Time: 12:20
 */

public class LvProductDetailsAdpater extends BaseAdapter {

    private Context mContext;
    private List<OrderDetailsRequest> mList;

    public LvProductDetailsAdpater(Context context, List<OrderDetailsRequest> list) {
        this.mContext = context;
        this.mList = list;

    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_lv_product_details, null);
            viewHolder = new ViewHolder();
            viewHolder.img_product = (ImageView) convertView.findViewById(R.id.img_product);
            viewHolder.tv_product_name = (TextView) convertView.findViewById(R.id.tv_product_name);
            viewHolder.tv_product_price = (TextView) convertView.findViewById(R.id.tv_product_price);
            viewHolder.tv_product_attribute = (TextView) convertView.findViewById(R.id.tv_product_attribute);
            viewHolder.tv_product_num = (TextView) convertView.findViewById(R.id.tv_product_num);
            viewHolder.original_price = (TextView) convertView.findViewById(R.id.original_price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (mList.get(position).getCoverImg() != null) {
            ImageUtils.loadImageView(mContext, mList.get(position).getCoverImg(), viewHolder.img_product);
        } else {
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, viewHolder.img_product);
        }

        viewHolder.tv_product_name.setText(mList.get(position).getProductTitle());
        viewHolder.tv_product_num.setText("x" + mList.get(position).getQuantity());
        viewHolder.tv_product_attribute.setText("已选：" + mList.get(position).getNameValue());
        String price = mList.get(position).getUnitPrice();
        String originalPrice = mList.get(position).getOriginalPrice();
        viewHolder.tv_product_price.setText("￥" + price);
        if (!price.equals(originalPrice)) {
            viewHolder.original_price.setText("￥" + originalPrice);
            viewHolder.original_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        }else {
            viewHolder.original_price.setText("");
        }
        return convertView;
    }

    class ViewHolder {

        ImageView img_product;
        TextView tv_product_name, tv_product_price, tv_product_attribute, tv_product_num, original_price;

    }
}